<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User_story_model
 *
 * @author Julio Arredondo
 */
class User_story_model extends CI_Model {
    //put your code here
    var $id_user_story;
    var $contenido;
    var $validado;
    var $comentario;
    var $id_back_log_item;

    public function create($id_back_log_item, $contenido){
        $this->contenido = $contenido;
        $this->id_back_log_item = $id_back_log_item;
        $resp = $this->db->insert("user_stories", $this);
        if(!$resp){
            throw new Exception("No se pudo insertar el User Story");
        }
        return $this->db->insert_id();
    }

    public function update($id_back_log_item, $id_user_story, $contenido){
        $this->contenido = $contenido;
        $this->id_user_story =  $id_user_story;
        $this->id_back_log_item = $id_back_log_item;
        $where = array("id_user_story" => $this->id_user_story);
        $this->db->where($where);
        $resp = $this->db->update("user_stories", $this);
        if(!$resp){
            throw new Exception("No se pudo actualizar el User Story");
        }
        return $this->id_user_story;
    }
    
    public function delete($id){ //TODO: hacer validaciones para inactivar
        $where = array("id_user_story" => $id);
        $data = array("activo" => 0); //se desactiva el campo activo
        $this->db->where($where);
        $resp = $this->db->update("user_stories", $data);
        if(!$resp){
            throw new Exception("No se pudo eliminar el User Story");
        }
        return $resp;
    }


    public function get_by_back_log($id_back_log_item){
        $where = array("id_back_log_item" => $id_back_log_item, "activo" => 1);
        $query = $this->db->get_where("user_stories", $where);
        return $query->result_array();
    }

    private function set_from_post() {
        $tmp_id = $this->input->post("id_user_story");
        if (!empty($tmp_id)) {
            $this->id_user_story = $tmp_id;
        }
        $this->contenido = $this->input->post("contenido");
        $this->comentario = $this->input->post("comentario");
        $this->id_back_log_item = $this->input->post("id_back_log_item");
    }

}
?>
